(function() {
  var leftMenuList = [
    {
      type: 'menu',
      name: 'index',
      src: 'personSave.html',
      text: i18next.t('index.personinfo')
    },
     {
      type: 'menu',
      name: 'index',
      src: 'timetable.html',
      text: i18next.t('index.mycoursetable')
    },
    {
      type: 'menu',
      name: 'index',
      src: 'teacher_worktime.html',
      text: i18next.t('index.worktime')
    },
    {
      type: 'menu',
      name: 'index',
      src: 'teacher_class.html',
      text: i18next.t('index.homeworkmanage')
    },
    {
      type: 'menu',
      name: 'index',
      src: 'mystudent.html',
      text: i18next.t('index.mystudent')
    },
     {
      type: 'menu',
      name: 'index',
      src: 'remind_set.html',
      text: i18next.t('index.notifyset')
    }
  ]
  function leftMenuInit(leftMenuList, curHtml) {
    var $listGroup = $('<ul class="menu-list"></ul>');
    $.each(leftMenuList, function (i, item) {
      if(item.type.indexOf('menu') > -1) {
        var $item = $('<li class="menu"><span class="text">'+ item.text +'</span></li>').appendTo($listGroup)
        if(i == 0) {
          $item.addClass('active');
        }

        if(item.type.indexOf('has-sub') > -1) {
          $item.addClass('has-sub');
        }

        if(item.type.indexOf('submenu') > -1) {
          $item.addClass('submenu');
        }


        if(item.src) {
          $item.on('click', function () {
            $(this).addClass('active').siblings().removeClass('active');
            // $('iframe').attr('src', item.src)
            locationHash(item.src)
          })
        }

        if(curHtml && item.src == curHtml) {
          $item.addClass('active').siblings().removeClass('active')
        }

      } else {
        $('<li class="title"><span class="text">'+ item.text +'</span></li>').appendTo($listGroup)
      }
    })
    $('.left-content').append($listGroup)
  }

  function locationHash(hashStr) {
    if(hashStr) {
      location.hash = hashStr.split('.html')[0]
    }

    if(location.hash) {
      $('iframe').attr('src', location.hash.substr(1) + '.html')
    } else {
      $('iframe').attr('src', 'personSave.html')
    }
  }

  locationHash()
  leftMenuInit(leftMenuList, location.hash.substr(1) + '.html');

  $('#J_logout').on('click', function () {
    ajaxMethod({
      url: '/api/user/logout',
      method: 'post',
      success: function (data, status) {
        if(status.code == 0) {
          location.href = '/'
        }
      }
    })
  })

  function getlogo(){
    $.ajax({
      type:'get',
      url:'/api/organization/1',
      success:function (logo) {
        if(logo.data.agencyreferred)
          $('.logo').text(logo.data.agencyreferred);
        else {
          if (logo.data.orgname)
            $('.logo').text(logo.data.orgname);
        }
      }
    });
  }
  getlogo();

  function  getCurrentUser() {
    $.ajax({
      type:'get',
      url:'/api/user/current',
      success:function (user) {
        $('.login-user').text(i18next.t('index.currentuser') + '： '+(user.data.account || user.data.number));
        $('#J_logout').show()
      }
    });
  }
  getCurrentUser();
}());
